package com.hu.zhcc.shiro.controller.Reponsitory;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.Reponsitory.RHealth;
import com.hu.zhcc.shiro.service.Responsitory.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/***
 * 健康方案Controller
 */
@RestController
public class HealthController {

    @Autowired
    private HealthService healthService;

    /***
     * 分页及条件查询
     * @param offset
     * @param limit
     * @param healCoding
     * @param healType
     * @return
     */
    @GetMapping("healAll")
    private Page<RHealth> healAll(String offset, String limit, String healCoding, String healType) {
        RHealth health = new RHealth(healCoding, healType);
        Map map = new HashMap();
        map.put("pageIndex", offset);
        map.put("num", limit);
        return healthService.healthPage(map, health);
    }

    /***
     * 根据id进行删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteHeal{id}")
    private int deleteHealById(@PathVariable("id") int id) {
        return healthService.deleteByPrimaryKey(id);
    }

    /***
     * 添加
     * @param rhealth
     * @return
     */
    @PostMapping("addHeal")
    private int addheal(@RequestBody RHealth rhealth) {
        healthService.insertSelective(rhealth);
        int id = rhealth.getId();
        if (id < 10) {
            rhealth.setHealCoding("FA00" + rhealth.getId());
            return healthService.updateByPrimaryKeySelective(rhealth);
        } else if (id >= 10) {
            rhealth.setHealCoding("FA0" + rhealth.getId());
            return healthService.updateByPrimaryKeySelective(rhealth);
        } else {
            rhealth.setHealCoding("FA" + rhealth.getId());
            return healthService.updateByPrimaryKeySelective(rhealth);
        }
    }

    /***
     * 修改前查询
     * @param id
     * @return
     */
    @PostMapping("healSeleUpdate{id}")
    private RHealth seleUpdate(@PathVariable("id") int id) {
        return healthService.selectRHealByid(id);
    }

    /***
     * 修改
     * @param healmodel
     * @return
     */
    @PutMapping("rHealEdit")
    private int healedit(@RequestBody RHealth healmodel) {
        return healthService.updateByPrimaryKeySelective(healmodel);
    }
}
